<template>
    <div ref="con" class="con" :style="{fontSize:fontSize, color:color}">
        <div ref="item" class="item" :style="{transform: 'translateX(' + left1 + 'px)'}">{{content}}</div>
        <div class="item" v-if="isScroll" :style="{transform: 'translateX(' + left2 + 'px)'}">{{content}}</div>
    </div>
</template>
<script>
export default {
    props: {
        fontSize: String,
        color: String,
        content: String,
        time: Number
    },
    data () {
        return {
            isScroll: false,
            conWidth: 0,
            itemWidth: 0,
            left1: 0,
            left2: 0,

            now: 0,
            
        }
    },
    methods: {
        init () {
            this.isScroll = true
            this.left1 = 0
            this.left2 = 0
        },

        play () {
             requestAnimationFrame(() => {
                this.play()

                let now = Date.now()
                // console.log(now - this.now)
                if ((now - this.now) > this.time) {
                    this.now = now
                    
                     this.move()
                   
                    
                }
            })
        },
        move () {
            this.left1 --
            this.left2 --

            if (this.left1 < -((this.itemWidth * 2) - this.conWidth)) {
                this.left1 = this.conWidth
                
            }

            if (this.left2 < - this.itemWidth * 2) {
                this.left2 = 0
                
            }

            
           
        }
    },
    mounted () {
        // requestAnimationFrame
        
        this.conWidth = this.$refs.con.offsetWidth
        this.itemWidth =  this.$refs.item.offsetWidth
        if (this.itemWidth >= this.conWidth) {
            this.init()
            this.play()
        }
    }
}
</script>
<style scoped>
.con{
    width: 100%; position: relative; overflow: hidden;
}
.item{
    display: inline-block;vertical-align: middle; padding: 0 10px;
}
</style>